package com.yupi.picturebackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.picturebackend.exception.BusinessException;
import com.yupi.picturebackend.exception.ErrorCode;
import com.yupi.picturebackend.model.entity.Picture;
import com.yupi.picturebackend.model.entity.PictureThumb;
import com.yupi.picturebackend.model.entity.User;
import com.yupi.picturebackend.service.PictureService;
import com.yupi.picturebackend.service.PictureThumbService;
import com.yupi.picturebackend.mapper.PictureThumbMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
* @author 伍立权
* @description 针对表【picture_thumb(图片点赞)】的数据库操作Service实现
* @createDate 2025-04-20 17:06:01
*/
@Service
public class PictureThumbServiceImpl extends ServiceImpl<PictureThumbMapper, PictureThumb>
    implements PictureThumbService{

    @Resource
    private PictureService pictureService;

    /**
     *  点赞
     * @param pictureId
     * @param loginUser
     * @return
     */
    @Override
    public int doPictureThumb(long pictureId, User loginUser) {
        // 判断实体是否存在，根据类别获取实体
        Picture picture = pictureService.getById(pictureId);
        if (picture == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已点赞
        long userId = loginUser.getId();
        // 每个用户串行点赞
        // 锁必须要包裹住事务方法
        PictureThumbService pictureThumbService = (PictureThumbService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()) {
            return pictureThumbService.doPictureThumbInner(userId, pictureId);
        }
    }

    /**
     * 帖子点赞（内部服务）
     * @param userId
     * @param pictureId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int doPictureThumbInner(long userId, long pictureId) {
        PictureThumb pictureThumb = new PictureThumb();
        pictureThumb.setUserId(userId);
        pictureThumb.setPictureId(pictureId);
        QueryWrapper<PictureThumb> thumbQueryWrapper = new QueryWrapper<>(pictureThumb);
        PictureThumb oldPostThumb = this.getOne(thumbQueryWrapper);
        boolean result;
        // 已点赞
        if (oldPostThumb != null) {
            result = this.remove(thumbQueryWrapper);
            if (result) {
                // 点赞数 - 1
                result = pictureService.update()
                        .eq("id", pictureId)
                        .gt("thumbNum", 0)
                        .setSql("thumbNum = thumbNum - 1")
                        .update();
                return result ? -1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未点赞
            result = this.save(pictureThumb);
            if (result) {
                // 点赞数 + 1
                result = pictureService.update()
                        .eq("id", pictureId)
                        .setSql("thumbNum = thumbNum + 1")
                        .update();
                return result ? 1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }
}




